
$(document).ready(function() {
	
	var oriMail = "";
	try {
		User = $.parseJSON($.cookie('User'));
		oriMail = User.mail;
	} catch(e) {
		console.log("已知异常："+e);
	}
	
	$("#bm-send1").click(function(e) {
        var param = {
			captchaType: 2,
			captchaWay: 0,
			captchaUse: 605,
		};
		AQ.callAQ("applyCaptcha.do", param, function(result) {
			countDown($("#bm-send1")[0], 60);
		}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
	$("#bm-send2").click(function(e) {
		var mail1 = $.trim($("#bm-mail-1").text());
        var mail = $("#bm-mail").val();
		var mailReg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
		if(!mailReg.test(mail)) {
			Utils.msgDialog("×  邮箱格式不正确！");
			return false;
		} else if(mail == mail1) {
			Utils.msgDialog("×  新邮箱不能和原邮箱相同！");
			return false;
		}
		
        var param = {
			captchaType: 2,
			captchaWay: mail,
		};
		if($.trim(oriMail) == "") {	//绑定邮箱
			param.captchaUse = 605;
		} else {	//更换邮箱
			param.captchaUse = 606;
		}
		AQ.callAQ("applyCaptcha.do", param, function(result) {
			countDown($("#bm-send2")[0], 60);
			$("#bm-mail").prop("readOnly", true);
		}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
	$("#bm-submit").click(function(e) {
        var code1 = $("#bm-code1").val();
		var code2 = $("#bm-code2").val();
		
		var param = null;
		var codeReg = /^[0-9]{6}$/;
		if($.trim(oriMail) == "") {	//绑定邮箱
			if(!codeReg.test(code2)) {
				Utils.msgDialog("×  邮箱验证码格式不正确！");
				return false;
			}
			param = {
				captchaCode: code2,
			};
		} else {	//更换邮箱
			if(!codeReg.test(code1)) {
				Utils.msgDialog("×  原邮箱验证码格式不正确！");
				return false;
			} else if(!codeReg.test(code2)) {
				Utils.msgDialog("×  新邮箱验证码格式不正确！");
				return false;
			}
			param = {
				captchaCode: code1,
				newCaptchaCode: code2,
			};			
		}
		
		AQ.callAQ("bindMail.do", param, function(result) {
			Utils.msgDialog("操作成功！");
			location.replace("user-center.html");
		}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
});